package fr.utbm.aiechat.activities;

import fr.utbm.aiechat.Network;
import fr.utbm.aiechat.structures.Channel;
import fr.utbm.aiechat.structures.User;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ChannelsTabActivity extends ListActivity
{
	Channel chanSelected = null;
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setListAdapter(Network.getInstance().Channels);
		
		final ListView lv = getListView();
		lv.setTextFilterEnabled(true);
		
		lv.setOnItemClickListener(new OnItemClickListener()
			{
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id)
				{
					// When clicked, show a toast with the TextView text
					//Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
					chanSelected = (Channel) lv.getItemAtPosition(position);
					showDialog(position);
				}
		});

	}
	protected Dialog onCreateDialog(final int id)
	{
	    Dialog dialog;
	    	
    	final CharSequence[] items = {"Rejoindre Salon"};
    	
    	//Important dans le builder mettre Activity.this pour avoir les attributs de l'objet
		AlertDialog.Builder builder = new AlertDialog.Builder(ChannelsTabActivity.this);
		builder.setTitle("Actions");
		builder.setItems(items, new DialogInterface.OnClickListener() {
		    public void onClick(DialogInterface dialog, int item) {
		    	switch (item) {
		    		case 0 : 
		    			//send UDP broadcast request to obtain the real channel object (with userList)
		    			Network.getInstance().sendRequestChanObject(chanSelected.getUniqueID());
		    			AieChatActivity.tabHost.setCurrentTab(0);
		    	}
		    }
		});
		
		dialog = builder.create();
	    return dialog;
	}
}
